﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CRM_Web.Models;

namespace CRM_Web.Controllers
{
    public class RolesController : ApiController
    {
        // GET: api/Roles
        /// <summary>
        /// 表格数据请求
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
       public IHttpActionResult getTableList(int page,int limit,string Name)
        {
            using(CRMDBEntities db=new CRMDBEntities())
            {
                var tablelist = db.Roles.AsEnumerable();
                if (!String.IsNullOrEmpty(Name))
                {
                    tablelist = tablelist.Where(p => p.Name.Contains(Name));
                }
                var list = tablelist.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name
                }).ToList();
                int count = list.Count();
                return Json(new
                {
                    count = count,
                     data = list.OrderByDescending(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList()
                });
            }
        }
        /// <summary>
        /// 添加角色处理
        /// </summary>
        /// <returns></returns>
        public IHttpActionResult addRole([FromBody] Roles add)
        {
            using(CRMDBEntities db=new CRMDBEntities())
            {
                var role = db.Roles.SingleOrDefault(p => p.Name == add.Name);
                if (role != null)
                {
                    return Json(new { code = -1, msg = "此角色名称已经存在，请示重新输入角色名称！" });
                }
                else
                {
                    db.Roles.Add(add);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "添加成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "添加失败！" });
                    }
                }
               
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="aaa"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult delRoles([FromBody] Roles aaa)
        {
            using(CRMDBEntities db=new CRMDBEntities())
            {
                var a1 = db.Roles.FirstOrDefault(p => p.Id == aaa.Id);
                if (a1 != null)
                {
                    var a2 = db.RoleModules.Where(p => p.RoleId == a1.Id&&p.ModuleId!=null).ToList();
                    int count = a2.Count();
                    if (count < 0)
                    {
                        db.Roles.Remove(a1);
                        int n = db.SaveChanges();
                        if (n > 0)
                        {
                            return Json(new { code = 0, msg = "删除成功！" });
                        }
                        else
                        {
                            return Json(new { code = -1, msg = "删除失败！" });
                        }
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "此角色已关联权限不能删除！" });
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "此角色不存在！" });
                }
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult submitRole([FromBody] Roles roles)
        {
            using(CRMDBEntities db = new CRMDBEntities ())
            {
                var role = db.Roles.FirstOrDefault(p => p.Id == roles.Id);
                if (role.Name == roles.Name)
                {
                    return Json(new { code = -1, msg = "此角色名称已存在,不能重复！" });
                }
                else
                {
                    role.Name = roles.Name;
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "编辑成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "编辑失败！" });
                    }
                }
               
            }
        }
        /// <summary>
        ///模块编辑
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult EditRM(string Id, string Value)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                int n = 0;
                int id = Convert.ToInt32(Id);
                if (Value != null)
                {
                    string[] strArray = Value.Split(','); //字符串转数组
                    List<int> abc = new List<int>();
                    foreach (var g in strArray)
                    {
                        abc.Add(Convert.ToInt32(g));
                    }
                    var datalist = db.RoleModules.Where(p => p.RoleId == id).ToList();
                    foreach (var item in datalist)
                    {
                        db.RoleModules.Remove(item);
                        n += db.SaveChanges();
                    }
                    foreach (var i in abc)
                    {
                        var dd = new RoleModules()
                        {
                            RoleId = id,
                            ModuleId = i
                        };
                        db.RoleModules.Add(dd);
                        n += db.SaveChanges();
                    }
                }
                else
                {
                    var datalist = db.RoleModules.Where(p => p.RoleId == id).ToList();
                    foreach (var item in datalist)
                    {
                        db.RoleModules.Remove(item);
                        n += db.SaveChanges();
                    }
                }
                if (n >= 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
        /// <summary>
        /// 获取所有模块
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAllModules()
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var datalist = db.Modules.Where(p => p.ParentId == 0).ToList();
                var bb = datalist.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight,
                    Ops = p.Ops,
                    Int0 = p.Int0
                }).ToList();
                List<ModuleList> data = new List<ModuleList>();
                foreach (var item in bb)
                {
                    var list = db.Modules.Where(p => p.ParentId == item.Id).ToList();
                    var cc = list.Select(p => new Modules()
                    {
                        Id = p.Id,
                        Name = p.Name,
                        ParentId = p.ParentId,
                        Path = p.Path,
                        Weight = p.Weight,
                        Ops = p.Ops,
                        Int0 = Convert.ToInt32(p.Int0)
                    }).ToList();
                    var aa = new ModuleList()
                    {
                        Id = item.Id,
                        Name = item.Name,
                        ParentId = item.ParentId,
                        Path = item.Path,
                        Weight = item.Weight,
                        Ops = item.Ops,
                        Int0 = Convert.ToInt32(item.Int0),
                        children = cc
                    };
                    data.Add(aa);
                };
                return Json(new { data = data, code = 0 });
            }
        }
        /// <summary>
        /// 获取自身模块
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult GetMyModules([FromBody]Roles role)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var datalist = db.RoleModules.Where(p => p.RoleId == role.Id).ToList();
                var list = new List<int>();
                foreach (var item in datalist)
                {
                    list.Add(Convert.ToInt32(item.ModuleId));
                }
                return Json(new { data = list, code = 0 });
            }
        }



    }
}
